<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="变量声明var类似于 JavaScript 中的 var，它可以定义变量 最大的不同是 Dart 中 var 变量一旦赋值，类型便会确定，不能改变其类型 dynamic 和 ObjectObject 是 Dart 所有对象的根基类，也就是说所有类型都是 Object 的子类（包含 Function 和 NULL），所以任何类型的数据都可以赋值给 Object 声明的对象，dynamic 与 var">
<meta property="og:type" content="article">
<meta property="og:title" content="Dart-语言简介">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2020/08/26/Dart-%E8%AF%AD%E8%A8%80%E7%AE%80%E4%BB%8B/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="变量声明var类似于 JavaScript 中的 var，它可以定义变量 最大的不同是 Dart 中 var 变量一旦赋值，类型便会确定，不能改变其类型 dynamic 和 ObjectObject 是 Dart 所有对象的根基类，也就是说所有类型都是 Object 的子类（包含 Function 和 NULL），所以任何类型的数据都可以赋值给 Object 声明的对象，dynamic 与 var">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-08-26T08:21:21.000Z">
<meta property="article:modified_time" content="2020-08-26T08:21:40.175Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="Dart">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2020/08/26/Dart-%E8%AF%AD%E8%A8%80%E7%AE%80%E4%BB%8B/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>Dart-语言简介 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/08/26/Dart-%E8%AF%AD%E8%A8%80%E7%AE%80%E4%BB%8B/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Dart-语言简介
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-08-26 16:21:21 / Modified: 16:21:40" itemprop="dateCreated datePublished" datetime="2020-08-26T16:21:21+08:00">2020-08-26</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/Dart/" itemprop="url" rel="index"><span itemprop="name">Dart</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="变量声明"><a href="#变量声明" class="headerlink" title="变量声明"></a>变量声明</h2><h3 id="var"><a href="#var" class="headerlink" title="var"></a><code>var</code></h3><p>类似于 JavaScript 中的 <code>var</code>，它可以定义变量</p>
<p>最大的不同是 Dart 中 <code>var</code> 变量一旦赋值，类型便会确定，不能改变其类型</p>
<h3 id="dynamic-和-Object"><a href="#dynamic-和-Object" class="headerlink" title="dynamic 和 Object"></a><code>dynamic</code> 和 <code>Object</code></h3><p><code>Object</code> 是 Dart 所有对象的根基类，也就是说所有类型都是 <code>Object</code> 的子类（包含 Function 和 NULL），所以任何类型的数据都可以赋值给 <code>Object</code> 声明的对象，<code>dynamic</code> 与 <code>var</code> 一样都是关键词，声明的变量可以赋值任意对象。</p>
<h4 id="相同之处"><a href="#相同之处" class="headerlink" title="相同之处"></a>相同之处</h4><p><code>dynamic</code> 与 <code>Object</code> 声明的变量可以在后期改变赋值类型</p>
<h4 id="不同之处"><a href="#不同之处" class="headerlink" title="不同之处"></a>不同之处</h4><p><code>dynamic</code> 声明的对象编译器会提供所有可能的组合，而 <code>Object</code> 声明的对象只能使用 <code>Object</code> 的属性和方法，否则编译器会报错</p>
<h3 id="final-和-const"><a href="#final-和-const" class="headerlink" title="final 和 const"></a><code>final</code> 和 <code>const</code></h3><p>如果你从未打算更改一个变量，那么使用 <code>final</code> 或 <code>const</code>，不是 <code>var</code>，也不是一个类型。一个 <code>final</code> 变量只能被设置一次。被 <code>final</code> 或者 <code>const</code> 修饰的变量，变量类型可以省略</p>
<h4 id="不同之处-1"><a href="#不同之处-1" class="headerlink" title="不同之处"></a>不同之处</h4><p><code>const</code> 变量是一个编译时常量，<code>final</code> 变量在第一次使用时被初始化</p>
<h2 id="函数"><a href="#函数" class="headerlink" title="函数"></a>函数</h2><p>Dart 是一种真正的面向对象的语言，所以即使是函数也是对象，并且有一个类型 Function。这意味着函数可以赋值给变量或作为参数传递给其他函数，这是函数式编程的典型特征</p>
<h3 id="函数声明"><a href="#函数声明" class="headerlink" title="函数声明"></a>函数声明</h3><p>Dart 函数声明如果没有显式声明返回值类型时会默认当做 <code>dynamic</code> 处理，注意，函数返回值没有类型推断</p>
<h3 id="可选的位置参数"><a href="#可选的位置参数" class="headerlink" title="可选的位置参数"></a>可选的位置参数</h3><p>包装一组函数参数，用<code>[]</code>标记为可选的位置参数</p>
<h3 id="可选的命名参数"><a href="#可选的命名参数" class="headerlink" title="可选的命名参数"></a>可选的命名参数</h3><p>定义函数时，使用<code>&#123;param1, param2,...&#125;</code>，用于指定命名参数</p>
<p>调用函数时，可以传参。如果传参，就必须使用指定形参的名称。例如：<code>paramName: value</code></p>
<h2 id="异步支持"><a href="#异步支持" class="headerlink" title="异步支持"></a>异步支持</h2><p>Dart 类库有非常多的返回 <code>Future</code> 或者 <code>Stream</code> 对象的函数。这些函数被称为 <strong>异步函数</strong> ：它们只会在设置好一些耗时操作之后返回，比如像IO操作。而不是等到这个操作完成</p>
<p><code>async</code> 和 <code>await</code> 关键词支持了异步编程，允许我们写出和同步代码很像的异步代码</p>
<h3 id="Future"><a href="#Future" class="headerlink" title="Future"></a><code>Future</code></h3><p><code>Future</code> 与 JavaScript 中的 <code>Promise</code> 非常相似，表示一个异步操作的最终完成（或失败）及其结果值的表示。简单来说，它就是用于处理异步操作的，异步处理成功了就执行成功的操作，异步操作失败了就捕获错误或者停止后续操作。一个 <code>Future</code> 只会对应一个结果，要么成功，要么失败</p>
<p><code>Future</code> 的所有 API 的返回值仍然是一个 <code>Future</code> 对象，所以可以很方便的进行链式调用</p>
<h4 id="Future-then"><a href="#Future-then" class="headerlink" title="Future.then"></a><code>Future.then</code></h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Future.delayed(new Duration(seconds: 2), ()&#123;</span><br><span class="line">    return &quot;hi.world&quot;;</span><br><span class="line">&#125;).then((data)&#123;</span><br><span class="line">    print(data);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h4 id="Future-catchError"><a href="#Future-catchError" class="headerlink" title="Future.catchError"></a><code>Future.catchError</code></h4><p>如果异步任务发生错误，我们可以在 <code>catchError</code> 中捕获错误</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Future.delayed(new Duration(seconds: 2), ()&#123;</span><br><span class="line">    return &quot;hi.world&quot;;</span><br><span class="line">&#125;).then((data)&#123;</span><br><span class="line">    print(data);</span><br><span class="line">&#125;).catchError((e)&#123;</span><br><span class="line">    &#x2F;&#x2F; 执行失败会走到这里</span><br><span class="line">    print(e);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>不是只有 <code>catchError</code> 回调才能捕获错误，<code>then</code> 方法还有一个可选参数 <code>onError</code>，我们也可以用它来捕获异常</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">Future.delayed(new Duration(seconds: 2), ()&#123;</span><br><span class="line">    return &quot;hi.world&quot;;</span><br><span class="line">&#125;).then((data)&#123;</span><br><span class="line">    print(data);</span><br><span class="line">&#125;, onError: (e)&#123;</span><br><span class="line">    print(e);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="Future-whenComplete"><a href="#Future-whenComplete" class="headerlink" title="Future.whenComplete"></a><code>Future.whenComplete</code></h4><p>有些时候，我们会遇到无论异步任务执行成功或失败都需要做一些事的场景，比如在网络请求前弹出加载对话框，在请求结束后关闭对话框。</p>
<p>这种场景，有两种方法，第一种是分别在 <code>then</code> 或 <code>catch</code> 中关闭一下对话框，第二种就是使用 <code>Future</code> 的 <code>whenComplete</code> 回调</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">Future.delayed(new Duration(seconds: 2), ()&#123;</span><br><span class="line">    return &quot;hi.world&quot;;</span><br><span class="line">&#125;).then((data)&#123;</span><br><span class="line">    &#x2F;&#x2F; 执行成功会走到这里</span><br><span class="line">    print(data);</span><br><span class="line">&#125;, onError: (e)&#123;</span><br><span class="line">    &#x2F;&#x2F; 执行失败会走到这里</span><br><span class="line">    print(e);</span><br><span class="line">&#125;).whenComplete(()&#123;</span><br><span class="line">    &#x2F;&#x2F; 无论成功或失败都会走到这里</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<h4 id="Future-wait"><a href="#Future-wait" class="headerlink" title="Future.wait"></a><code>Future.wait</code></h4><p>它接收一个 <code>Future</code> 数组参数，只有数组中所有 <code>Future</code> 都执行成功后，才会触发 <code>then</code> 的成功回调，只要有一个 <code>Future</code> 执行失败，就会触发错误回调。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Future.wait([</span><br><span class="line">    &#x2F;&#x2F; 2秒后返回结果</span><br><span class="line">    Future.delayed(new Duration(seconds: 2), ()&#123;</span><br><span class="line">        return &#39;hello&#39;;</span><br><span class="line">    &#125;),</span><br><span class="line">    &#x2F;&#x2F; 4秒后返回结果</span><br><span class="line">    Future.delayed(new Duration(seconds:4), ()&#123;</span><br><span class="line">        return &#39; world&#39;;</span><br><span class="line">    &#125;)</span><br><span class="line">]).then((results)&#123;</span><br><span class="line">    print(result[0]+result[1]);</span><br><span class="line">&#125;).catchError((e)&#123;</span><br><span class="line">    print(e);</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="Async-await"><a href="#Async-await" class="headerlink" title="Async/await"></a><code>Async/await</code></h3><h4 id="回调地狱（Callback-Hell）"><a href="#回调地狱（Callback-Hell）" class="headerlink" title="回调地狱（Callback Hell）"></a>回调地狱（Callback Hell）</h4><p>如果代码中有大量的异步逻辑，并且出现大量异步任务依赖其它异步任务的结果时，必然会出现 <code>Future.then</code> 回调中套回调情况</p>
<p>可以感受一下，如果业务逻辑中由大量异步依赖的情况，将会出现在回调里面套回调的情况，过多的嵌套会导致代码可读性下降以及出错率提高，并且非常难维护，这个问题被形象的称为回调地狱（Callback Hell）</p>
<h4 id="使用-async-await-消除-callback-hell"><a href="#使用-async-await-消除-callback-hell" class="headerlink" title="使用 async/await 消除 callback hell"></a>使用 <code>async/await</code> 消除 callback hell</h4><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Future&lt;String&gt; login(String userName, String pwd)&#123;</span><br><span class="line">    &#x2F;&#x2F; 用户登录</span><br><span class="line">&#125;</span><br><span class="line">Future&lt;String&gt; getUserInfo(String id)&#123;</span><br><span class="line">    &#x2F;&#x2F; 获取用户信息</span><br><span class="line">&#125;</span><br><span class="line">Future saveUserInfo(String userInfo)&#123;</span><br><span class="line">    &#x2F;&#x2F; 保存用户信息</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">task() async&#123;</span><br><span class="line">    try&#123;</span><br><span class="line">        String id &#x3D; await login(&#39;alice&#39;, &#39;*****&#39;);</span><br><span class="line">        String userInfo &#x3D; await getUserInfo(id);</span><br><span class="line">        await saveUserInfo(userInfo);</span><br><span class="line">    &#125;catch(e)&#123;</span><br><span class="line">        &#x2F;&#x2F; 错误处理</span><br><span class="line">        print(e);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p><code>async</code> 用来表示函数是异步的，定义的函数会返回一个 <code>Future</code> 对象，可以使用 then 方法添加回调函数</p>
</li>
<li><p><code>await</code> 后面是一个 <code>Future</code>，表示等待该异步任务完成，异步完成后才会往下走；<code>await</code> 必须出现在 <code>async</code> 函数内部</p>
</li>
<li><p>其实，无论是在 JavaScript 还是 Dart 中，<code>async/await</code> 都只是一个语法糖，编译器或解释器最终都会将其转化为一个 Promise(Future) 的调用链</p>
</li>
</ul>
<h3 id="Stream"><a href="#Stream" class="headerlink" title="Stream"></a><code>Stream</code></h3><p><code>Stream</code> 也是用于接收异步事件数据，和 <code>Future</code> 不同的是，它可以接收多个异步操作的结果（成功或失败）。也就是说，在执行异步任务时，可以通过多次触发成功或失败事件来传递结果数据或错误异常。<code>Stream</code> 常用于会多次读取数据的异步任务场景，如网络内容下载、文件读写等</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">Stream.fromFutures([</span><br><span class="line">    &#x2F;&#x2F; 1秒后返回结果</span><br><span class="line">    Future.delayed(new Duration(seconds: 1), ()&#123;</span><br><span class="line">        return &#39;hello 1&#39;;</span><br><span class="line">    &#125;),</span><br><span class="line">    &#x2F;&#x2F; 抛出一个异常</span><br><span class="line">    Future.delayed(new Duration(seconds: 2), ()&#123;</span><br><span class="line">        throw AssertionError(&#39;Error&#39;);</span><br><span class="line">    &#125;),</span><br><span class="line">    &#x2F;&#x2F; 3秒后返回结果</span><br><span class="line">    Future.delayed(new Duration(seconds: 3), ()&#123;</span><br><span class="line">        return &#39;hello 3&#39;;</span><br><span class="line">    &#125;)</span><br><span class="line">]).listen((data)&#123;</span><br><span class="line">    print(data);</span><br><span class="line">&#125;, onError: (e)&#123;</span><br><span class="line">    print(e.message);</span><br><span class="line">&#125;, onDone: ()&#123;</span><br><span class="line"></span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>上面的代码依次会输出</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">I&#x2F;flutter (17666): hello 1</span><br><span class="line">I&#x2F;flutter (17666): Error</span><br><span class="line">I&#x2F;flutter (17666): hello 3</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/Dart/" rel="tag"># Dart</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/Dart-%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%EF%BC%88OOP%EF%BC%89/" rel="prev" title="Dart-面向对象（OOP）">
      <i class="fa fa-chevron-left"></i> Dart-面向对象（OOP）
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2020/08/26/Flutter-%E5%88%9D%E8%AF%86%E7%AF%87/" rel="next" title="Flutter-初识篇">
      Flutter-初识篇 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%98%E9%87%8F%E5%A3%B0%E6%98%8E"><span class="nav-number">1.</span> <span class="nav-text">变量声明</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#var"><span class="nav-number">1.1.</span> <span class="nav-text">var</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#dynamic-%E5%92%8C-Object"><span class="nav-number">1.2.</span> <span class="nav-text">dynamic 和 Object</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%B8%E5%90%8C%E4%B9%8B%E5%A4%84"><span class="nav-number">1.2.1.</span> <span class="nav-text">相同之处</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%8D%E5%90%8C%E4%B9%8B%E5%A4%84"><span class="nav-number">1.2.2.</span> <span class="nav-text">不同之处</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#final-%E5%92%8C-const"><span class="nav-number">1.3.</span> <span class="nav-text">final 和 const</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%B8%8D%E5%90%8C%E4%B9%8B%E5%A4%84-1"><span class="nav-number">1.3.1.</span> <span class="nav-text">不同之处</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0"><span class="nav-number">2.</span> <span class="nav-text">函数</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%A3%B0%E6%98%8E"><span class="nav-number">2.1.</span> <span class="nav-text">函数声明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AF%E9%80%89%E7%9A%84%E4%BD%8D%E7%BD%AE%E5%8F%82%E6%95%B0"><span class="nav-number">2.2.</span> <span class="nav-text">可选的位置参数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%AF%E9%80%89%E7%9A%84%E5%91%BD%E5%90%8D%E5%8F%82%E6%95%B0"><span class="nav-number">2.3.</span> <span class="nav-text">可选的命名参数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%82%E6%AD%A5%E6%94%AF%E6%8C%81"><span class="nav-number">3.</span> <span class="nav-text">异步支持</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Future"><span class="nav-number">3.1.</span> <span class="nav-text">Future</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Future-then"><span class="nav-number">3.1.1.</span> <span class="nav-text">Future.then</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Future-catchError"><span class="nav-number">3.1.2.</span> <span class="nav-text">Future.catchError</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Future-whenComplete"><span class="nav-number">3.1.3.</span> <span class="nav-text">Future.whenComplete</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#Future-wait"><span class="nav-number">3.1.4.</span> <span class="nav-text">Future.wait</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Async-await"><span class="nav-number">3.2.</span> <span class="nav-text">Async&#x2F;await</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9B%9E%E8%B0%83%E5%9C%B0%E7%8B%B1%EF%BC%88Callback-Hell%EF%BC%89"><span class="nav-number">3.2.1.</span> <span class="nav-text">回调地狱（Callback Hell）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8-async-await-%E6%B6%88%E9%99%A4-callback-hell"><span class="nav-number">3.2.2.</span> <span class="nav-text">使用 async&#x2F;await 消除 callback hell</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Stream"><span class="nav-number">3.3.</span> <span class="nav-text">Stream</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
